<?php
class StepControllerNode {
	public $text;
	public $id;
	public $children;
	public $hasChildren;
}

class StepController extends CController {

	const PAGE_SIZE = 10;

	/**
	 * @var string specifies the default action to be 'list'.
	 */
	public $defaultAction = 'admin';

	/**
	 * @var CActiveRecord the currently loaded data model instance.
	 */
	private $_model;

	public function getSubmenu()
	{
		return array(
				'steps' => array(
						'icon' => Yii::app()->request->baseUrl . '/images/list.png',
						'label' => Yii::t('submenu', 'Manage steps'),
						'href' => array('steps')
					),
				'route' => array(
						'icon' => Yii::app()->request->baseUrl . '/images/list.png',
						'label' => Yii::t('submenu', 'Manage routes'),
						'href' => array('routes')
					)
		);
	}
	
	/**
	 * @return array action filters
	 */
	public function filters() {
		return array (
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules() {
		return array (
			array (
				'allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions' => array ('steps','routes'),
				'roles' => array('Steps.canAdmin')
			),
			array (
				'deny', // deny all users
				'users' => array (
					'*'
				)
			)
		);
	}

	/**
	 * Manages all models.
	 */
	public function actionSteps() {
		if ($_POST['json'])
		{
			// parse json string
			$arr = json_decode($_POST['json'], true);
			$steps = array();
			foreach (Step::model()->findAll() as $step)
			{
				$steps [$step->id] = $step;
			}
			
			$topics = array();
			foreach (Topic::model()->findAll() as $topic)
			{
				$topics [$topic->id] = $topic;
			}
			
			foreach ($arr as $arrstep)
			{
				$update = false;
				$step = null;
				if (isset($arrstep["id"]))
				{
					$step = $steps[$arrstep["id"]];
				} else {
					$step = new Step();
					$update = true;
				}
				if ($step->label != $arrstep["label"])
				{
					$step->label = $arrstep["label"];
					$update = true;
				}
				if ($step->description != $arrstep["description"])
				{
					$step->description = $arrstep["description"];
					$update = true;
				}
				if ($step->active != ($arrstep["active"] == "true"))
				{
					$step->active = $arrstep["active"] == "true" ? 0 : 1;
					$update = true;
				}
				
				if ($update)
				{
					if (!$step->save())
					{
						echo "<ul><li>" . $step->label ;
						echo CHtml::errorSummary($step);
						echo "</li></ul>";
					}
				}
				$arrstep["id"] = $step->id;
				
				$this->saveTopics($topics, $arrstep["items"], $step->id, null);
			}
			
		}
		
		$this->render('steps', array ());
	}

	
	private function saveTopics($topics, $arr, $step_id, $parent_id)
	{
		$idx = count($arr);
		foreach ($arr as $arrtopic)
		{
			$update = false;
			$topic = null;
			if (isset($arrtopic["id"]))
			{
				$topic = $topics[$arrtopic["id"]];
			} else {
				$topic = new Topic();
			}
			if($topic->label != $arrtopic["label"])
			{
				$topic->label = $arrtopic["label"];
				$update = true;
			}
			if($topic->description != $arrtopic["description"])
			{
				$topic->description = $arrtopic["description"];
				$update = true;
			}
			if($topic->active != ($arrtopic["active"] == "true"))
			{
				$topic->active = $arrtopic["active"] == "true" ? 1 : 0;
				$update = true;
			}
			if($topic->step_id != $step_id)
			{
				$topic->step_id = $step_id;
				$update = true;
			}
			if($topic->parent_id != $prent_id)
			{
				$topic->parent_id = $parent_id;
				$update = true;
			}
			if($topic->orderIndex != $idx)
			{
				$topic->orderIndex = $idx;
			}
			$idx--;
			if ($update)
			{
				if(!$topic->save())
				{
			Constants::print_var($arrtopic, false);
			Constants::print_var($topic, false);
			Constants::print_var($topic->active);
					echo "<ul><li>" . $topic->label ;
						echo CHtml::errorSummary($topic);
						echo "</li></ul>";
				}
			}
			$arrtopic["id"] = $topic->id;
			
			$this->saveTopics($topics, $arrtopic["items"], null, $topic->id);
		}
	}
	
	public function actionRoutes()
	{
		$this->render('routes', array ());
	}
}
?>
